<?php

namespace App\Http\Controllers\Component;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UploadController extends Controller
{
    public function uploader(Request $request)
    {
        $upload = $request->file;
        if ($upload->isValid()) {
            $path = $upload->store(date('ym'), 'attachment');

            return ['valid' => 1, 'message' => asset('attachment/'.$path)];
        }

        return ['valid' => 0, 'message' => '上传失败文件大小不能超过2MB'];
    }

    public function filesLists()
    {
        $files = glob('attachment/*/*');
        foreach ($files as $f) {
            $data[] = ['url' => asset("/".$f), 'path' => asset('/'.$f)];
        }
//返回数据 data为文件列表 page 为分页数据，可以使用 houdunwang/page 组件生成

        return ['valid'=>1,'data' => $data,'page'=>[]];
    }
}
